package com.rrjjg.app.core.aop;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import static com.rrjjg.app.core.Constants.*;


/**
 * ----------------------------------------------------------------------------- <br>
 * 功能 :  <br>
 * ----------------------------------------------------------------------------- <br>
 * 作者 : Vayne <br>
 * 时间 : 2022/8/11 17:11 <br>
 */
@Slf4j
@Aspect
public class LogAop {

    private final String pre = "app logs ";

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object handleMethod(ProceedingJoinPoint pj) throws Throwable {
        Object ans = null;
        JSONObject logparam = new JSONObject();
        try {
            MethodSignature ms = (MethodSignature) pj.getSignature();
            long stime = System.currentTimeMillis();
            Object[] args = pj.getArgs();
            logparam.put(LOG_CLA, pj.getTarget().getClass().getName());
            String methodName = ms.getMethod().getName();
            logparam.put(LOG_MET, methodName);
            logparam.put(LOG_PAR, args);
            ans = pj.proceed();
            logparam.put(LOG_RES, ans);
            logparam.put(LOG_TI, System.currentTimeMillis() - stime);
            log.info("{} : {}", pre, logparam);
        } catch (Throwable e) {
            log.error("param ={} ,{} error", logparam,pre, e);
            throw e;
        }
        return ans;
    }

}

